/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package spynx.desl.impl;

import org.eclipse.emf.ecore.EClass;

import spynx.desl.DeslPackage;
import spynx.desl.DeslPattern;
import spynx.desl.DeslProgram;
import spynx.desl.runtime.IExecutionContext;
import spynx.desl.runtime.IPatternMatchInfo;
import spynx.desl.runtime.impl.ExecutionContext;
import spynx.desl.runtime.matchinfos.impl.PatternMatchInfo;
import spynx.util.DeslRuntimeUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Program</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class DeslProgramImpl extends ElementImpl implements DeslProgram {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected DeslProgramImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return DeslPackage.Literals.DESL_PROGRAM;
	}

	@Override
	public void run(CharSequence input) {
		run(null, null, input);
	}

	@Override
	public void run(IExecutionContext context, IPatternMatchInfo matchInfo, CharSequence input) {
		if (context == null) context = ExecutionContext.createRootContext();
		if (matchInfo == null) matchInfo = new PatternMatchInfo();
		
		matchInfo.setInput(input);
		DeslRuntimeUtil.setMatchInfo(context, matchInfo);

		DeslPattern p = findChild("main");
		
		if (p != null)
			p.execute(context, null);
		else
			throw new RuntimeException("Pattern 'main' is not found.");
	}

} //DeslProgramImpl
